<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/11
 * Time: 15:23
 */

namespace app\admin\controller;

use app\admin\common\controller\Base;
use app\admin\common\model\User as UserModel;
use think\facade\Request;
use think\facade\Session;

//use app\admin\common\
class User extends Base
{
    public function login(){
        $this->view->assign('title','管理员登陆');
        return $this->view->fetch();
    }

    public function checkLogin(){
        $data = Request::param();
        $result = UserModel::where('email',$data['email'])->find();
        if (is_null($result) || sha1($data['password']) != $result['password']){
            $this->error('登陆失败，邮箱或者密码不正确');
        }
        Session::set('admin_id', $result['id']);
        Session::set('admin_name', $result['name']);
        Session::set('admin_level', $result['is_admin']);
        $this->success('登陆成功', 'admin/user/userList');
    }

    public function logout(){
        # 1.清除session
        Session::clear();
        # 2.退出登陆并跳转到登陆页面
        $this->success('退出成功', 'admin/user/login');
    }

    public function userList(){
        # 1.获取到当前用户的id和级别is_admin
        $data = [
            'admin_id'=>Session::get('admin_id'),
            'admin_level'=>Session::get('admin_level')
        ];
        # 2.获取当前用户的信息
        $userList = UserModel::where('id', $data['admin_id'])->select();
        # 3.如果是超级管理员，获取到全部信息
        if ($data['admin_level'] == 1){
            $userList = UserModel::select();
        }
        # 4.模板赋值
        $this->view->assign('title', '用户管理');
        $this->view->assign('empty', '<span style="color: red;">没有任何数据</span>');
        $this->view->assign('userList', $userList);
        # 5.渲染出用户列表的模板
        return $this->view->fetch();
    }

    public function userEdit(){
        $userId = Request::param('id');
        $userInfo = UserModel::find($userId);
        $this->view->assign('title', '编辑用户');
        $this->view->assign('userInfo', $userInfo);
        return $this->view->fetch();
    }
}